#encoding=utf8
import datetime
import os


def join_list(lst,sep=u","):
    """
    数组转化成指定分隔符的字符串形式
    """
    try :
        if lst :
            return  (len(lst)==1 and str(lst[0]) or sep.join([str(e) for e in lst]))
    except Exception,e :
        print e
    return ""

def remove_none(lst):
    """
    去除数组中的空元素
    """
    if lst :
        return [e for e in lst if e is not None and  e!=u'' and e!='']
    return []

def date_range_array(start,end):
    """
    根据起始时间产生一个连续的区间范围数组
    """
    arr=[]
    if isinstance(start, datetime.date) and isinstance(end, datetime.date) :
        minv,maxv=start,end
        if start>end :
            minv,maxv=end,start
        
        while minv<=maxv:
            arr.append(minv)
            minv +=datetime.timedelta(days=1)  
    return arr

def split_to_int_array(str,sep=","):
    """
    将数字类型用逗号分割的字符串,如"1,2,3,4,5"转化为数组形式[1,2,3,4,5]
    """
    try:
        if str :
            return [int(c) for c in str.split(sep)]
    except Exception,e:
        print e
    return []

def date_region_intersect(start,end,start1,end1):
    """
    返回日期的交集区间[start,end] [start1,end1]
    """
    if start>end :
        start,end=end,start
    if start1>end1 :
        start1,end1=end1,start1
    min_date=max_date=None
    if (start<=start1 and end >=start1):
        min_date=start1
        max_date=(end1>=end and end or end1)
    elif (start<=end1 and end>=end1):
        min_date=(start1>=start and start1 or start)
        max_date=end1
    elif(start>=start1 and end<=end1):
        min_date=start
        max_date=end
    elif(start<=start1 and end>=end1):
        min_date=start1
        max_date=end1
    else :
        return []
    return [min_date,max_date]

def create_file_not_exists(filename):
    """
    创建不存在的文件,可以是相对路径也可以是绝对路径,并返回绝对的文件名称
    """
    if not os.path.exists(filename):
        dirName, baseName = os.path.split(filename)
        if not os.path.isabs(dirName) :
            dirName = os.path.abspath(dirName)
        if (not os.path.exists(dirName)) or (not os.path.isdir(dirName)):
            os.makedirs(dirName)
        filename = os.path.join(dirName,baseName) 
    
    return filename

class OrgType:
    PLATFORM=0
    VENDOR=1
    MERCHANT=2
    
#将一个对象列表中的某个属性，用一个连接符组合为一个字符串
def join_to_str(objs,attr,split):
    str = []
    for obj in objs:
        attr_str = getattr(obj,attr,'')
        if attr_str != '':
            str.append(attr_str)
    if len(str)==0:
        return ""
    else:
        return split.join(str)
# 字符串 转换为 日期 
def str_to_date(str,format):
    return datetime.datetime.strptime(str,format)
# 日期转换为字符串
def date_to_str(dt,format):
    return dt.strftime(format)
#字符串转换为日期，字符串是YYYY-mm-dd格式的
def hyphen_to_date(str):
    return str_to_date(str,'%Y-%m-%d')
# 日期转换为字符串，格式：YYYY-mm-dd
def date_to_hyphen(date):
    return date_to_str(date,'%Y-%m-%d')
def today():
    return datetime.date.today()
def yestoday():
    return datetime.date.today()-datetime.timedelta(days=1)
class AjaxResponse():
    code = 200
    message="成功"
    def to_dict(self):
        return {'code':self.code,'message':self.message}
